﻿using Orther.Model.param;
using Orther.Service.Miyao;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using static Dapper.SqlMapper;

namespace Orther.BLL.Miyao
{
    public class CarBLL
    {
        private CarService carService = new CarService();
        public TData<List<qx_car>> GetPageList(CarParam param, Pagination pagination)
        {
            TData<List<qx_car>> obj = new TData<List<qx_car>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = carService.GetPageList(param, pagination);
            obj.Tag = 1;
            obj.Total = pagination.TotalCount;
            return obj;
        }

        public async Task<TData<List<qx_car>>> GetList(CarParam param, int? topNum)
        {
            TData<List<qx_car>> obj = new TData<List<qx_car>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = await carService.GetList(param, topNum);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<qx_car>> GetObject(CarParam param)
        {
            TData<qx_car> obj = new TData<qx_car>();
            obj.Data = await carService.GetObject(param);
            obj.Tag = 1;
            return obj;
        }


        public async Task<TData> SaveForm(qx_car entity)
        {
            TData obj = new TData();
            if (entity == null)
            {
                obj.Message = "qx_car对象为空！";
                return obj;
            }

            if (entity.carno == null)
            {
                obj.Message = "操作失败，车牌号不能为空！";
                return obj;
            }

            CarParam paramCheckCar = new CarParam();
            paramCheckCar.carno = entity.carno;
            paramCheckCar.NotEqualId = entity.Id;
            List<qx_car> checkCarList = await carService.GetList(paramCheckCar,null);
            if (checkCarList.Count > 0)
            {
                obj.Message = "车牌号【" + entity.carno + "】已经存在，不能重复添加！";
                return obj;
            }

            //新增操作
            if (entity.Id == Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                //初始化主键值
                entity.Id = Guid.NewGuid();
                entity.IsDeleted = 0;
                //初始化字段的默认值
                List<object[]> defaultCols = new List<object[]>();
                defaultCols.Add(new object[] { "CreateDate", DateTime.Now });
                defaultCols.Add(new object[] { "CreateID", UserContext.Current.UserId });
                defaultCols.Add(new object[] { "Creator", UserContext.Current.UserName });
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + defaultCols[2][1] + "添加了一条车辆信息，车牌号称为:【" + entity.carno + "】。";
                oLog.Category = "添加";
                oLog.MenuModel = "车辆资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                carService.SaveForm(entity, oLog, defaultCols);
            }
            //更新操作
            else
            {
                List<object[]> defaultCols = new List<object[]>();
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "更新了一条车辆信息资料，" +
                    "车牌号变更为:【" + entity.carno + "】。" +
                    "客户名称设置为:【" + entity.kname + "】" +
                    "车型设置为:【" + entity.chexing + "】";
                oLog.Category = "修改";
                oLog.MenuModel = "车辆资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                carService.UpdateForm(entity, oLog, defaultCols);
            }


            obj.Tag = 1;

            return obj;
        }


        public async Task<TData> deleteFrom(Guid id)
        {
            TData obj = new TData();
            //第一步：获取要删除的企业名称
            CarParam param = new CarParam();
            param.Id = id;
            qx_car entity = await carService.GetObject(param);
            entity.IsDeleted = 1;
            //第二步：日志信息操作
            operatelog oLog = new operatelog();
            oLog.Log_Id = Guid.NewGuid();
            oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "删除了车牌号为：【" + entity.carno + "】的车辆信息。";
            oLog.Category = "删除";
            oLog.MenuModel = "车辆资料";
            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            carService.UpdateForm(entity, oLog);
            obj.Tag = 1;
            return obj;
        }

    }
}
